const axios = require('axios');

// Google OAuth2配置
const config = {
  clientId: '82716260351-eunjm8imnr6pc4djgnoaa9mvdoa08nsh.apps.googleusercontent.com', // 开发环境
  clientSecret: 'YOUR_CLIENT_SECRET', // 需要从Google Cloud Console获取
  redirectUri: 'http://localhost:3000/auth/google/callback',
  scope: 'openid email profile',
};

console.log('🔑 Google OAuth2 配置信息');
console.log('Client ID:', config.clientId);
console.log('重定向URI:', config.redirectUri);
console.log('权限范围:', config.scope);
console.log('\n📋 获取Access Token的步骤:');
console.log('1. 在浏览器中访问以下URL:');
console.log(`   https://accounts.google.com/o/oauth2/v2/auth?` +
  `client_id=${config.clientId}&` +
  `redirect_uri=${encodeURIComponent(config.redirectUri)}&` +
  `scope=${encodeURIComponent(config.scope)}&` +
  `response_type=code&` +
  `access_type=offline&` +
  `prompt=consent`);

console.log('\n2. 授权后，您会被重定向到回调URL，URL中会包含授权码');
console.log('3. 使用授权码调用以下API获取Access Token:');
console.log('\ncurl -X POST https://oauth2.googleapis.com/token \\');
console.log('  -H "Content-Type: application/x-www-form-urlencoded" \\');
console.log('  -d "client_id=' + config.clientId + '" \\');
console.log('  -d "client_secret=' + config.clientSecret + '" \\');
console.log('  -d "code=AUTHORIZATION_CODE" \\');
console.log('  -d "grant_type=authorization_code" \\');
console.log('  -d "redirect_uri=' + config.redirectUri + '"');

console.log('\n⚠️  注意:');
console.log('- 需要从Google Cloud Console获取Client Secret');
console.log('- 确保重定向URI已在Google Cloud Console中配置');
console.log('- Access Token有效期通常为1小时');
console.log('- 建议使用Refresh Token来获取新的Access Token');

// 测试现有Token是否有效
async function testExistingToken() {
  const existingToken = 'ya29.A0AS3H6NxHLey-eJ4EU5dV2VqJeEdR0vfgMQqIAinGGgD2CQJzSe7A_nurjfMVFD2ZkCmbTlcR9Ngiit5B1vrpF_xz4TG9l_PSV3kRri7OUH-kOmwy5oJx3J5iHEl67qcRWDAlAm5KJpxp0DxC5HxQvKg298MPs7VU15E235RV1QyPcGtbSq4UVNV9MxsTHMs2q7Z5G9K4781IixfddwYBJsM6Y1JYAwOEOHF54javwf8eWlypcxhWDjqH3lc627ejNaiVngdquFIraCgYKAX8SARUSFQHGX2MipjSptcn64x4Ni2LmBekckg0275';
  
  try {
    console.log('\n🧪 测试现有Token...');
    const response = await axios.get('https://www.googleapis.com/oauth2/v2/userinfo', {
      headers: {
        'Authorization': `Bearer ${existingToken}`,
      },
    });
    
    console.log('✅ Token仍然有效!');
    console.log('用户信息:', response.data);
  } catch (error) {
    if (error.response && error.response.status === 401) {
      console.log('❌ Token已过期或无效 (401错误)');
      console.log('需要获取新的Access Token');
    } else {
      console.log('❌ 其他错误:', error.message);
    }
  }
}

// 运行测试
testExistingToken(); 